//
//  HHSemiPresentAnimator.m
//  Pod
//
//  Created by Basic on 2017/3/23.
//  Copyright © 2017年 green. All rights reserved.
//

#import "HHSemiPresentAnimator.h"
#import <BCFoundation/UIImage+BCHelper.h>
#import <BCFoundation/BCFoundationUtils.h>


@interface HHSemiPresentAnimator ()
@property (nonatomic, assign, getter=isPresented) BOOL presented;//判断当前动画是弹出还是消失
/** 背景view */
@property (nonatomic, strong) UIImageView *bgImgv;
/** 灰色蒙层 */
@property (nonatomic, strong) UIImageView *maskImgv;
@end

@implementation HHSemiPresentAnimator
#pragma mark - system
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)dealloc
{
    BCLogDealloc();
}

#pragma mark - getter
-(UIImageView *)bgImgv {
    if (!_bgImgv) {
        _bgImgv = [[UIImageView alloc] init];
    }
    return _bgImgv;
}
-(UIImageView *)maskImgv {
    if (!_maskImgv) {
        _maskImgv = [[UIImageView alloc] init];
        _maskImgv.backgroundColor =  [UIColor.blackColor colorWithAlphaComponent:0.5];
    }
    return _maskImgv;
}

#pragma mark - UIViewControllerTransitioningDelegate
- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    self.presented = YES;
    return self;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    self.presented = NO;
    return self;
}

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    if(self.isPresented){
        [self animationForPresentView:transitionContext];
    } else {
        [self animationForDismissView:transitionContext];
    }
}

//自定义弹出动画
- (void)animationForPresentView:(id<UIViewControllerContextTransitioning> )transitionContext
{
    UIViewController *fromPage = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *presentView = [transitionContext viewForKey:UITransitionContextToViewKey];
    //将执行的View添加到containerView
    [transitionContext.containerView addSubview:presentView];
    UIImage *shotImg = [UIImage bc_snapshotImageFromView:fromPage.view];
    //添加背景
    if (!self.bgImgv.superview) {
        self.bgImgv.image = shotImg;
        self.bgImgv.frame = presentView.bounds;
        self.maskImgv.frame = presentView.bounds;
        UIViewController *toVC = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        if ([toVC isKindOfClass:[UINavigationController class]]) {
            //present UINavigationController
            UINavigationController *toNavc = (UINavigationController *)toVC;
            [toNavc.topViewController.view insertSubview:self.maskImgv atIndex:0];
            [toNavc.topViewController.view insertSubview:self.bgImgv atIndex:0];
        } else if ([toVC isKindOfClass:[UIViewController class]]) {
            //present UIViewController
            [toVC.view insertSubview:self.maskImgv atIndex:0];
            [toVC.view insertSubview:self.bgImgv atIndex:0];
        }
    }
    //事件分发
    if (shotImg && [self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorTo:didShotImg:)]) {
        [self.toDelegate HHSemiPresentAnimatorTo:self didShotImg:shotImg];
    }
    if ([self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorToWillPresent:)]) {
        [self.toDelegate HHSemiPresentAnimatorToWillPresent:self];
    }
    //开始转场动画
//    self.bgImgv.alpha = 0;
    self.maskImgv.alpha = 0;
    presentView.alpha = 0.0;
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//        self.bgImgv.alpha = 1.0;
        self.maskImgv.alpha = 1.0;
        presentView.alpha = 1.0;
        if ([self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorToPresenting:)]) {
            [self.toDelegate HHSemiPresentAnimatorToPresenting:self];
        }
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
        if ([self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorToCompletion:)]) {
            [self.toDelegate HHSemiPresentAnimatorToCompletion:self];
        }
    }];
}

//自定义消失动画
- (void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    if ([self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorToWillDismiss:)]) {
        [self.toDelegate HHSemiPresentAnimatorToWillDismiss:self];
    }
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.maskImgv.alpha = 0;
        dismissView.alpha = 0;
        if ([self.toDelegate respondsToSelector:@selector(HHSemiPresentAnimatorToDismissing:)]) {
            [self.toDelegate HHSemiPresentAnimatorToDismissing:self];
        }
    } completion:^(BOOL finished) {
        [dismissView removeFromSuperview];
        [transitionContext completeTransition:YES];
        [self.bgImgv removeFromSuperview];
        [self.maskImgv removeFromSuperview];
    }];
}
@end
